module.exports = app => {
  const DataTypes = app.Sequelize
  const sequelize = app.model
  const attributes = {
    id: {
      type: DataTypes.UUID,
      allowNull: false,
      primaryKey: true,
      defaultValue: DataTypes.UUIDV1,
      comment: '主键 唯一标识'
    },
    channelId: {
      type: DataTypes.STRING(32),
      comment: '课程栏目id',
      field: 'channel_id'
    },
    categoryId: {
      type: DataTypes.STRING(32),
      comment: '课程类别id',
      field: 'category_id'
    },
    termId: {
      type: DataTypes.STRING(32),
      comment: '课程分期id',
      field: 'term_id'
    },
    name: {
      type: DataTypes.STRING(128),
      comment: '课程名称'
    },
    bigPhoto: {
      type: DataTypes.STRING(255),
      comment: '课程图片',
      field: 'big_photo'
    },
    teacherId: {
      type: DataTypes.STRING(32),
      comment: '教师id',
      field: 'teacher_id'
    },
    teacherName: {
      type: DataTypes.STRING(64),
      comment: '教师名字',
      field: 'teacher_name'
    },
    schoolName: {
      type: DataTypes.STRING(64),
      comment: '学校名字',
      field: 'school_name'
    },
    schoolSN: {
      type: DataTypes.STRING(32),
      comment: '学校标志',
      field: 'school_sn'
    },
    courseId: {
      type: DataTypes.STRING(32),
      comment: '课程id',
      field: 'course_id'
    },
    evaluateCount: {
      type: DataTypes.INTEGER,
      comment: '评论数量',
      field: 'evaluate_count'
    },
    avgMark: {
      type: DataTypes.STRING(10),
      comment: '平均评分',
      field: 'avg_mark'
    },
    startTime: {
      type: DataTypes.DATE,
      comment: '开课时间',
      field: 'start_time'
    },
    endTime: {
      type: DataTypes.DATE,
      comment: '结课时间',
      field: 'end_time'
    },
    createTime: {
      type: DataTypes.DATE,
      comment: '创建日期',
      field: 'create_time'
    },
    createUser: {
      type: DataTypes.UUID,
      comment: '创建用户',
      field: 'create_user'
    },
    lastTime: {
      type: DataTypes.DATE,
      comment: '修改日期',
      field: 'last_time'
    },
    lastUser: {
      type: DataTypes.UUID,
      comment: '修改用户',
      field: 'last_user'
    },
    deleteFlag: {
      type: DataTypes.BOOLEAN,
      comment: '是否删除（1是，0否）',
      field: 'delete_flag',
      allowNull: false,
      defaultValue: false
    }
  }
  const options = {
    tableName: 'mooc_course'
  }
  const Course = sequelize.define('course', attributes, options)
  Course.associate = function() {
    app.model.Course.belongsTo(app.model.CourseColumn, { foreignKey: 'channelId', targetKey: 'channelId' }) // 一门课程属于一种栏目
    app.model.Course.belongsTo(app.model.CourseCategory, { foreignKey: 'categoryId', targetKey: 'categoryId' }) // 一门课程属于一种分类
    app.model.Course.belongsTo(app.model.Teacher, { foreignKey: 'teacherId', targetKey: 'teacherId' }) // 一门课程属于一位老师
    app.model.Course.hasMany(app.model.CourseTerminfo, { foreignKey: 'courseId' }) // 一门课程对应多个期数
  }
  return Course
}
